package com.haierp.service.logistics.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jetty.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.support.json.JSONUtils;
import com.haierp.exception.ErpCommonException;
import com.haierp.model.ErpOrder;
import com.haierp.model.ShippingOrder;
import com.haierp.service.IShippingOrderService;
import com.haierp.service.logistics.IXloboService;
import com.haierp.util.HttpClientUtil;
import com.haierp.util.MapUtil;
import com.haierp.util.Md5Util;

import net.sf.json.JSON;
import net.sf.json.JSONObject;
@Service("xloboService")
public class XloboServiceImpl implements IXloboService {
	@Autowired
	private IShippingOrderService shippingOrderService;
	
	public static final String  SecretKey = "APvYM8Mt5Xg1QYvker67VplTPQRx28Qt/XPdY9D7TUhaO3vgFWQ71CRZ/sLZYrn97w==";
	public static final String  APPKEY = "10cd0ae9-f139-404e-86bb-6851b69c3145";
	public static final String  AccessToken = "AOJeHVbxlXfDtq0m944mhg3SuuZPEfyaxGaef/v1zcbUuZM18btuWU0JWFFqdEzqKQ==";
	
	
	public static final String  TestSecretKey = "APvYM8Mt5Xg1QYvker67VplTPQRx28Qt/XPdY9D7TUhaO3vgFWQ71CRZ/sLZYrn97w==";
	public static final String  TestAPPKEY = "68993573-E38D-4A8A-A263-055C401F9369";
	public static final String  TestAccessToken = "ACiYUZ6aKC48faYFD6MpvbOf73BdE9OV5g15q1A6Ghs+i/XIawq/9RHJCzc6Y3UNxA==";

	
	
	 public static void main(String[] args) throws Exception  {  
			Map<String, Object> param = new HashMap<String, Object>();
			Map<String, Object> paramData = new HashMap<String, Object>();
			Map<String, Object> billSenderInfoparamData = new HashMap<String, Object>();
			Map<String, Object> billReceiverInfoparamData = new HashMap<String, Object>();
			Map<String, Object> billSupplyInfoparamData = new HashMap<String, Object>();
			Map<String, Object> good = new HashMap<String, Object>();
			List<Map<String, Object>> billCategoryList = new ArrayList<>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			paramData.put("BusinessNo", "12345690");
			paramData.put("Weight", "1.0");
			paramData.put("Comment", "测试备注");
			paramData.put("LogisticId", 122);//波特兰站点
			paramData.put("LogisticVersion", sdf.format(new Date()));//最新版本
			paramData.put("LineTypeId", 1);//个人线路
			paramData.put("IsContainTax", 1);//是否包税
			billSenderInfoparamData.put("Name","miss Gu");
			billSenderInfoparamData.put("Address", "9755 sw commerce cir, b3, wilsonville, or 97070");
			billSenderInfoparamData.put("Phone","6513998004");
			paramData.put("BillSenderInfo", billSenderInfoparamData);
			billReceiverInfoparamData.put("Name", "王侃");
			billReceiverInfoparamData.put("Province", "浙江省");
			billReceiverInfoparamData.put("City", "杭州市");
			billReceiverInfoparamData.put("District", "西湖区");
			billReceiverInfoparamData.put("Address", "文三西路东部软件园");
			billReceiverInfoparamData.put("Phone", "15088735320");
			paramData.put("BillReceiverInfo", billReceiverInfoparamData);
			billSupplyInfoparamData.put("OrderCode", "O010004D1806241636411076020002");
			billSupplyInfoparamData.put("TradingNo", "O010004D1806241636411076020002");
			billSupplyInfoparamData.put("ChannelName", "如花渠道");
			paramData.put("BillSupplyInfo", billSupplyInfoparamData);
			good.put("CategoryId", 159);
			good.put("CategoryVersion", sdf.format(new Date()));
			good.put("Count", 1);
			good.put("UnitPrice", 100.00);
			good.put("ProductName", "洁面奶");
			good.put("Brand", "雅诗兰黛");
			billCategoryList.add(good);
			paramData.put("BillCategoryList", billCategoryList);
			JSONObject jsonObject = JSONObject.fromObject(paramData);
			String paramStr = jsonObject.toString();
			System.out.println(paramStr);
			String sign = Md5Util.towMd5(toLowCase(SecretKey+jsonObject+SecretKey));
			//String sign1 = Md5Util.towMd5(toLowCase(SecretKey+SecretKey));
			param.put("method", "xlobo.labels.createNoVerification");//接口名称
			//param.put("method", "xlobo.catalogue.get");//接口名称
			param.put("v", "1.0");
			param.put("access_token", AccessToken);
			param.put("msg_param", jsonObject.toString());//参数
			param.put("client_id", APPKEY);//参数
			param.put("sign", sign);//参数
			JSONObject jsonObjectParam = JSONObject.fromObject(param);
			JSONObject jsonObjectBack = HttpClientUtil.post("http://bill.open.xlobo.com/api/router/rest", null, param, "1");
			System.out.println(jsonObjectBack);
		  /*  Map<String, Object> param = new HashMap<String, Object>();
			Map<String, Object> paramData = new HashMap<String, Object>();
			Map<String, Object> good = new HashMap<String, Object>();
			List<Map<String, Object>> ProductList = new ArrayList<>();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			paramData.put("BusinessNo", "12345690");
			paramData.put("ChannelName", "IRUHUA");
			paramData.put("OrderCode", "321432453254366565");
			paramData.put("ReceiverName", "王侃");
			paramData.put("ReceiverProvince", "河南省");
			paramData.put("ReceiverCity", "南阳市");
			paramData.put("ReceiverDistrict", "邓州市");
			paramData.put("ReceiverAddress", "人民路公路管理局");
			paramData.put("ReceiverMobile", "15088735320");
			paramData.put("ReceiverPostCode", "474150");//邮编
			paramData.put("PaymentPrice", "10.00");
			paramData.put("OrderPrice", "10.00");
			good.put("SkuName", "清洁洗面奶");
			good.put("SendCountryName", "美国");
			good.put("SecondLevelCategoryId", 2);//二级分类
			good.put("CategoryVersion", sdf.format(new Date()));
			good.put("Num", 1);
			good.put("UnitPrice", 100);
			good.put("Brand", "雅诗兰黛");
			good.put("Weight", 1.0);
			ProductList.add(good);
			paramData.put("ProductList", ProductList);
			JSONObject jsonObject = JSONObject.fromObject(paramData);
			String paramStr = jsonObject.toString();
			System.out.println(paramStr);
			String sign = Md5Util.towMd5(toLowCase(TestSecretKey+jsonObject+TestSecretKey));
			param.put("method", "xlobo.labels.importorder");//接口名称
			param.put("v", "1.0");
			param.put("access_token", TestAccessToken);
			param.put("msg_param", jsonObject.toString());//参数
			param.put("client_id", TestAPPKEY);//参数
			param.put("sign", sign);//参数
			JSONObject jsonObjectParam = JSONObject.fromObject(param);
			JSONObject jsonObjectBack = HttpClientUtil.post("http://114.80.87.216:8082/api/router/rest", null, param, "1");
			System.out.println(jsonObjectBack);*/
		 
	    }


	@Override
	public String createOrder(Long id, Long cpmpanyId) {
		ShippingOrder shippingOrder = shippingOrderService.selectById(id);
		if (shippingOrder == null) {
			throw new ErpCommonException("发货单号异常");
		}
		List<ErpOrder> ErpOrderList = shippingOrderService.queryShippingOrderDetail(shippingOrder.getErpOrderId());

		
		Map<String, Object> param = new HashMap<String, Object>();
		Map<String, Object> paramData = new HashMap<String, Object>();
		Map<String, Object> billSenderInfoparamData = new HashMap<String, Object>();
		Map<String, Object> billReceiverInfoparamData = new HashMap<String, Object>();
		List<Map<String, Object>> billCategoryList = new ArrayList<>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		paramData.put("BusinessNo", shippingOrder.getShippingNo());
		paramData.put("Weight", shippingOrder.getSkuWeight());
		paramData.put("Comment",shippingOrder.getRemark());
		paramData.put("LogisticId", 122); //洛杉矶站点
		paramData.put("LogisticVersion", sdf.format(new Date()));
		paramData.put("LineTypeId", 1);// (个人食品保健渠道)
		billSenderInfoparamData.put("Name","miss Gu");
		billSenderInfoparamData.put("Address", "9755 sw commerce cir, b3, wilsonville, or 97070");
		billSenderInfoparamData.put("Phone","6513998004");
		paramData.put("BillSenderInfo", billSenderInfoparamData);
		billReceiverInfoparamData.put("Name", shippingOrder.getReceiver());
		billReceiverInfoparamData.put("Province", shippingOrder.getReceiverState());
		billReceiverInfoparamData.put("City", shippingOrder.getReceiverCity());
		billReceiverInfoparamData.put("District", shippingOrder.getReceiverDistrict());
		billReceiverInfoparamData.put("Address", shippingOrder.getAddressDetail());
		billReceiverInfoparamData.put("Phone", shippingOrder.getTelephone());
		paramData.put("BillReceiverInfo", billReceiverInfoparamData);
		ErpOrderList.forEach(erpOrder -> {
			Map<String, Object> goods = new HashMap<String, Object>();
			goods.put("CategoryId", 13);
			goods.put("CategoryVersion", sdf.format(new Date()));
			goods.put("Count", erpOrder.getQuantity());
			goods.put("UnitPrice", erpOrder.getSalePrice());
			goods.put("ProductName", erpOrder.getItemName());
			goods.put("Brand", erpOrder.getBrand());
			billCategoryList.add(goods);
		});
		paramData.put("billCategoryList", billCategoryList);
		JSONObject jsonObject = JSONObject.fromObject(paramData);
		String paramStr = jsonObject.toString();
		System.out.println(paramStr);
		String sign = "";
		try {
			sign = Md5Util.towMd5(toLowCase(SecretKey+jsonObject+SecretKey));
		} catch (Exception e) {
			e.printStackTrace();
		}
		param.put("method", "xlobo.labels.createNoVerification");//接口名称
		param.put("v", "1.0");
		param.put("access_token", AccessToken);
		param.put("msg_param", jsonObject.toString());//参数
		param.put("client_id", APPKEY);//参数
		param.put("sign", sign);//参数
		JSONObject jsonObjectParam = JSONObject.fromObject(param);
		JSONObject jsonObjectBack = HttpClientUtil.post("http://bill.open.xlobo.com/api/router/rest", null, param, "1");
		
		
		return null;
	}


	@Override
	public void shippingTrack(String shipperOrderNo) throws ParseException {
		// TODO Auto-generated method stub
		
	}

	public static String toLowCase(String str) {
		String lowStr = str.toLowerCase(); 
		return Base64.getEncoder().encodeToString(lowStr.getBytes());
	}
	
	
	
}
